package com.xy.p2p.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.SQLQuery;

import com.xy.common.page.Pagination;
import com.xy.core.dao.impl.BaseDaoImpl;
import com.xy.p2p.dao.TmemberassetaccountDao;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;

public class TmemberassetaccountDaoImpl extends BaseDaoImpl<Tmemberassetaccount> implements TmemberassetaccountDao {
	public TmemberassetaccountDaoImpl() {
		super(Tmemberassetaccount.class);
	}
   
   
	@Override
	public List<Tmemberassetaccount> getTmemberassetaccount(Map<String, Object> map) {
		Object[] values = map.values().toArray();
		List<Tmemberassetaccount> localList = findByHql(getHql(map), values);
		return localList;
	}

	@Override
	public Pagination getTmemberassetaccountByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		Object[] values = map.values().toArray();
		return findByPage(getHql(map), values, pageNo, pageSize);
	}

	private String getHql(Map<String, Object> map) {
		StringBuffer str = new StringBuffer("from Tmemberassetaccount tt");
		if(map.size()>0)str.append(" where ");
		Set<String> set = map.keySet();
		StringBuffer where = new StringBuffer();
		for (String name : set) {
			if (where.length() > 0)
				where.append(" and  ");
			where.append("tt.").append(name).append("=?");
		}
		return str.append(where.toString()).toString();
	}   
	
	@Override
	public double getTotalAsset(Tmember tmember){
//		String sql = "select SUM(mibmoney) from t_tenderrecord where memberid = "+tmember.getMemberid()+" and fiid in (select fiid from t_financeinfo where status >= 7 and status != 8 and targettype != 8 )";
		String sql = "select SUM(mibmoney) from t_tenderrecord where memberid = "+tmember.getMemberid()+" and fiid in (select fiid from t_financeinfo where targettype != 8 )";
		List result = executeSQL(sql);
		if(null == result.get(0))
			return 0.0;
		else
			return Double.parseDouble(result.get(0).toString());
	}


	@Override
	public double getTotalBorrow(Tmember tmember) {
		String sql ="select sum(mibmoney) from t_tenderrecord where fiid in (select fiid from t_financeinfo where memberid = "+tmember.getMemberid()+" )";
		List result = executeSQL(sql);
		if(null == result.get(0))
			return 0.0;
		else
			return Double.parseDouble(result.get(0).toString());
	}



	@Override
	public double getTotalIncome(Tmember tmember) {
		String sql ="select sum(tranamount) from t_memberassetaccountdetails where memberid = "+tmember.getMemberid()+" and borrowing = 1 and trantype = 3 and otherside not in (select fiid from t_financeinfo where targettype = 8)";
		List result = executeSQL(sql);
		if(null == result.get(0))
			return 0.0;
		else
			return Double.parseDouble(result.get(0).toString());
	}
	
	//执行sql
	private List executeSQL(String sql) {
		System.out.println(sql);
		SQLQuery query = getSession().createSQLQuery(sql);
		List  result = query.list();
		return result;
	}
	
}